package _go

import (
	"strconv"
	"strings"
)

func countAndSay(n int) string {

	if n <= 1 {
		return "1"
	}
	var resTmp []string
	var res string
	tmp := []string{"1"}

	for count := 1; count < n; count++ {
		for len(tmp) >= 1 {
			// fmt.Printf("tmp %s \n",tmp)
			if len(tmp) == 1 {
				resTmp = append(resTmp, "1")
				resTmp = append(resTmp, tmp[0])
				tmp = []string{}
				break
			}
			//双指针取区间值
			for i, j := 0, 1; i < len(tmp); j++ {
				if tmp[i] != tmp[j] {
					resTmp = append(resTmp, strconv.Itoa(len(tmp[i:j])), tmp[i])
					tmp = tmp[j:]
					break
				}
				//注意边界值判断
				if j+1 == len(tmp) {
					resTmp = append(resTmp, strconv.Itoa(len(tmp)))
					resTmp = append(resTmp, tmp[i])
					tmp = []string{}
					break
				}
			}
		}
		tmp = resTmp
		res = strings.Join(resTmp, "")
		//  fmt.Printf("res %s \n",res)
		resTmp = []string{}
	}
	return res
}
